// pages/list/list.js
Page({

  /**
   * 页面的初始数据
   */
  // tupian:"/pages/img/menu_blur.png"
  data: {
    List: [
      { id: 1, text: '自提' },
      { id: 2, text: '外送' },
    ],
    tabs: [
      { title: '人气Top', anchor: 'a',  },
      { title: '厚乳拿铁', anchor: 'b', },
      { title: '经典拿铁', anchor: 'c', },
      { title: '圣诞限定', anchor: 'd', },
      { title: '大师咖啡', anchor: 'e', },
      { title: '环球经典', anchor: 'f', },
      { title: '欢乐下午茶', anchor: 'g',},
      { title: '轻乳好茶', anchor: 'h', },
      { title: '小鹿茶榜单', anchor: 'i', },
      { title: '瑞纳冰', anchor: 'j',},
      { title: '甜品坚果', anchor: 'k', },
      { title: '烘焙轻食', anchor: 'l', },
      { title: '特价零食', anchor: 'm',},
      { title: 'NFC果汁', anchor: 'n', },
      { title: '经典饮品', anchor: 'o', },
    ],
    indexId: 0,
    toTitle:"title-c",
    scrollTop:0,
    top:[],
    
    },
    // 左侧点击事件
    jumpIndex(e) {
      let index = e.currentTarget.dataset.menuindex;
      let anchor = e.currentTarget.dataset.anchor;
      let that = this
      that.setData({
        indexId: index,
        toTitle: "title-" + anchor
      });
      //可以设置定位事件
   
    },
    xuanran:function(e){
    console.log(e)
    },
  scrollToLeft(res){
    // console.log("scrollToLeft-res:" + JSON.stringify(res) + JSON.stringify(this.data.top));
    // let top=res.detail.scrollTop;
    this.setData({
      scrollTop: res.detail.scrollTop
    })
    var length = this.data.top.length;
    for(var i=0;i<this.data.top.length;i++){
      if (this.data.top[i] - this.data.top[0] <= this.data.scrollTop && (i < length - 1 && this.data.top[i + 1] - this.data.top[0] > this.data.scrollTop)){
       if(this.data.indexId!=i){
          this.setData({
            indexId: i,
          }); 
        }
      }
    }
    // console.log("top:"+top);
  },

    
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winHeight: res.windowHeight
        });
        var top2=new Array();
        for(var i=0;i<that.data.tabs.length;i++){
          wx.createSelectorQuery().select('#view-' + that.data.tabs[i].anchor).boundingClientRect(function (rect) {          
            var isTop=Number(rect.top);           
            top2.push(isTop);
            // console.log("view-c:" + JSON.stringify(rect));           
          }).exec();        
        }  
        that.setData({
          top: top2
        });
      } 
       
      
    });
    // 定位功能实现  
    wx.chooseAddress({
      success: (result) => {
        console.log(result)
        this.setData({
          cityName:result.countyName+result.detailInfo
        })
      },
    })
    // 连接数据库   
    wx.cloud.init()
    const db = wx.cloud.database()
    db.collection('counters').where({
      _openid: "o5mU34-pWA5cgwjom6edXbzB57p8"

    }).get({
      success: res => {
        this.setData({
          tabsList:res.data[1].tabsList
        })
        console.log('[数据库] [查询记录] 成功: ', res)
        console.log(res.data[1])

      },
   
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})